package com.vdong.restful.controller;


import com.vdong.restful.api.request.CurrencyParamRequest;
import com.vdong.restful.api.request.TemplateRequest;
import com.vdong.restful.api.response.Response;
import com.vdong.restful.service.TemplateApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.AbstractPageRequest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/template")
public class TemplateController {

    @Autowired
    private TemplateApiService templateService;

    /**
     * 添加模板
     * @param templateRequest
     * @param key
     * @return
     */
    @RequestMapping(value = "/addOneTeplateService.json", method = RequestMethod.POST)
    public Response addTeplates(@Valid @RequestBody TemplateRequest templateRequest, String key) {


        return templateService.add(templateRequest, key);

    }

    /**
     * 通过userid获取所有模板
     * @param currencyParamRequest
     * @param pagebale
     * @param key
     * @return
     */
    @RequestMapping(value = "/getListByUserIdService.json", method = RequestMethod.GET)
    public Response findByUserId(CurrencyParamRequest currencyParamRequest,
                                 Integer page, Integer size, String key) {

        Pageable pageable = new PageRequest(page, size);

        return templateService.findByUserId(currencyParamRequest.getUserId(), pageable, key);

    }

    /**
     * 通过userid获取在用模板
     * @param currencyParamRequest
     * @param key
     * @return
     */
    @RequestMapping(value = "/getOneByUserIdAndStateService.json")
    public Response findByUserIdAndState(
            @Validated(CurrencyParamRequest.FindByUserIdAndState.class) CurrencyParamRequest currencyParamRequest,
            String key) {

        return templateService.findByUserIdAndState(currencyParamRequest.getUserId(), currencyParamRequest.getState(),
                key);

    }

    @RequestMapping(value = "/getOneByTemplateIdService.json")
    public Response findByTemplateId(@Validated(CurrencyParamRequest.FindByTemplateId.class) CurrencyParamRequest currencyParamRequest,
                                     String key) {

        return templateService.findById(currencyParamRequest.getTemplateId(), key);

    }

    /**
     * 删除暂停使用的模板
     *
     * @param currencyParamRequest
     * @return
     */
    @RequestMapping(value = "/delOneStateByTemplateIdService.json")
    public Response deleteTemplate(@Validated(CurrencyParamRequest.FindByTemplateId.class) CurrencyParamRequest currencyParamRequest,
                                   String key) {

        return templateService.deleteByStateAndId(currencyParamRequest.getTemplateId(), key);
    }

    /**
     * 激活模板
     *
     * @param currencyParamRequest
     * @return
     */
    @RequestMapping(value = "/getOneStateByTemplateIdService.json")
    public Response saveTemplateStateById(
            @Validated(CurrencyParamRequest.SaveTemplateStateById.class) CurrencyParamRequest currencyParamRequest,
            String key) {

        return templateService.saveTemplateStateById(currencyParamRequest.getTemplateId(), key);
    }

}
